package com.cy.mall.web.comm;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cy.mall.comm.utils.JsonObjectMapper;
import com.cy.mall.model.Result;
import com.cy.mall.service.comm.FaceIDCertificateService;
import com.cy.mall.service.customer.CustomerService;
import com.cy.mall.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * faceID 人脸识别
 *
 * @author tanyilin
 *
 * @create 2019年2月21日 下午4:16:23
 */
@Slf4j
@RestController
@RequestMapping("/faceid")
@Api(tags = "faceID 人脸识别")
public class FaceIDCertificateController extends BaseController {

	@Value("${app.apply.page}")
	private String applyPageUrl;
	@Autowired
	private FaceIDCertificateService faceIDCertificateService;
	@Autowired
	private CustomerService customerService;
	
	@ApiOperation("请求bizToken")
	@GetMapping("/get/biz/token")
	public Result getBizToken() {

		Result result = faceIDCertificateService.getBizToken();
		return result;
	}
	
	@ApiOperation("查询结果")
	@GetMapping("/get/result")
	public Result getResult(@RequestParam(value = "bizToken") String bizToken) {
		Result result = faceIDCertificateService.getResult(bizToken);
		return result;
	}
	
	@ApiOperation("人脸识别流程结束通知回调")
	@GetMapping("/notify")
	public ModelAndView toNotify(@RequestParam(value = "BizToken") String BizToken) {
		
		Result<Map<String, Object>> result = faceIDCertificateService.getResult(BizToken);
		
		String detectInfo =  result.getData().get("DetectInfo").toString();
		
		Map<String, Object> detectInfoMap = JsonObjectMapper.read(detectInfo, Map.class);
		
		Map<String, Object> textMap = (Map<String, Object>) detectInfoMap.get("Text");
		
		customerService.saveFaceIdCheckResult(textMap, BizToken);
		
		return new ModelAndView("redirect:".concat(applyPageUrl));
	}
	
}
